package com.richapp.launcher.scene.conditions.time;

import android.util.Log;

import java.util.Date;

import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractTimeConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;
import com.richapp.launcher.scene.interfaces.ISceneTimeConditions;


public class SceneTimeDelayConditions extends SceneAbstractTimeConditions {

	private long startTime = 0;
	private long delayTime = 0;
    private int count = 0;
	
	public SceneTimeDelayConditions() {
		
	}

	@Override
	public int getConditionType() {
		return ISceneConditions.SCENE_CONDITION_TYPE_TIME;
	}

	@Override
	public int getConditionSubType() {
		return ISceneTimeConditions.SCENE_CONDITION_SUB_TYPE_TIME_DELAY;
	}

	@Override
	public boolean checkCondition(String sceneName) {
		SceneModel sceneModel = new SceneModel();
		SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
		sceneModel=sceneModelDao.getSceneModelOnly(sceneName);
		long currentTime = System.currentTimeMillis();
        if((currentTime-startTime) > delayTime && "true".equals(sceneModel.getStatusCycleFlag())){
			if(count == 0 ){
                Log.v("TEST","currentTime:"+currentTime);
                Log.v("TEST","startTime:"+startTime);
                Log.v("TEST","delayTime:"+delayTime);
                Log.v("TEST","时差:"+(currentTime-startTime));
                count++;
				return true;
			}
		}else if (count!=0){
			sceneModel.setStatusCycleFlag("true");
			sceneModelDao.add(sceneModel);
		}
		return false;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public long getDelayTime() {
		return delayTime;
	}

	public void setDelayTime(long delayTime) {
		this.delayTime = delayTime;
	}

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
